<?php
declare (strict_types = 1);

namespace app\controller\sys;

use think\Request;
use think\facade\View;
use app\model\Partner as PartnerModel;
use app\model\PartnerType as PartnerTypeModel;
use app\utils\ReUtils;

class Partner  extends Base
{
    protected $modelName="Honor";
    
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $params = $this->app->request->param();
        
        $list = PartnerModel::queryPage($params);

        return View::fetch('', ['list'=>$list]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save($id=0)
    {
        if ($this->app->request->isPost()) {
            $params = $this->app->request->param();

            if ($params['title'] == '') {
                $this->error("名称不能为空");
            }

            try {
                $id = $params['id'];
                unset($params['id']);

                if ($id != 0) {
                    PartnerModel::update($params, ['id' => $id]);
                } else {
                    PartnerModel::create($params);
                }

                return ReUtils::success();
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                return ReUtils::error($msg);
            }
        } else {
            if ($id != 0) {
                $data = PartnerModel::find($id);
            } else {
                $data = new PartnerModel();
                $data->status = 1;
            }

            $listTypes = PartnerTypeModel::select();

            View::assign('listTypes', $listTypes);
    
            View::assign('data', $data);
        
            return View::fetch();
        }
    }

    /**
     * 类别列表
     */
    public function typelist()
    {
        $list = PartnerTypeModel::select();

        return View::fetch('',[
            'list' => $list
        ]);
    }

    /**
     * 保存新建的资源
     *
     * @param  int  $id 
     * @return \think\Response
     */
    public function typesave($id = 0)
    {
        if ($this->app->request->isPost()) {
            $params = $this->app->request->param();

            if ($params['name'] == '') {
                $this->error("名称不能为空");
            }

            try {
                $id = $params['id'];
                unset($params['id']);

                if ($id != 0) {
                    PartnerTypeModel::update($params, ['id' => $id]);
                } else {
                    PartnerTypeModel::create($params);
                }

                return ReUtils::success();
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                return ReUtils::error($msg);
            }
        }
    }

    /**
     * 删除类别
     * @param int|array $id  info id
     * @return array
     */
    public function typedelete($id)
    {
        if ($this->request->isPost()) {
            if (PartnerTypeModel ::destroy($id)) {
                return ReUtils::success();
            } else {
                return ReUtils::error();
            }
        }
    }
}
